#!/bin/bash

################################
# this module defines some action about git proxy
# NOTE: this is a partial module of dfproxym
###############################

git_usage() {
  echo "Kind:  git"
  echo "  e.g. $prog git                                                      note: show git subcommand usage"
  echo "       $prog git instead                                              note: show git instead subcommand usage"
  echo "       $prog git instead status  [--local|--global|--system]          note: show status of git instead, default local"
  echo "       $prog git instead enable  [--local|--global|--system]          note: enable git instead, default local"
  echo "       $prog git instead disable [--local|--global|--system]          note: disable git instead, default local"
}

git_scope=''
get_git_scope_opt() {
  if has_opt '--local' "$@"; then
    git_scope='--local'
    return
  fi

  if has_opt '--global' "$@"; then
    git_scope='--global'
    return
  fi

  if has_opt '--system' "$@"; then
    git_scope='--system'
    return
  fi
}

execute_subcmd_git_instead() {
  local cmd=${1:-""}

  get_git_scope_opt "${@:2}"

  load_configs

  case "${cmd}" in
  status)
    _info "Current git ${git_scope:--local} scope insteadOf configurations:"
    echo ""
    
    # 显示当前配置
    local current_insteads
    current_insteads=$(git config --get-regexp ${git_scope} "^url\." 2>/dev/null | grep -i "insteadof" || true)
    if [[ -n "$current_insteads" ]]; then
      echo "$current_insteads"
    else
      echo "  No insteadOf configurations found"
    fi
    
    echo ""
    _info "Configured insteadOf mappings in config:"
    echo ""
    
    # 显示配置文件中定义的映射
    local insteads_from=()
    local insteads_to=()
    mapfile -t insteads_from < <(get_keys_grep_regex_from_cfg "git\.insteads\.[0-9]+\.from")
    mapfile -t insteads_to < <(get_keys_grep_regex_from_cfg "git\.insteads\.[0-9]+\.to")
    
    [[ "${#insteads_from[@]}" == "${#insteads_to[@]}" ]] || _err 'len(git.insteads.*.from) != len(git.insteads.*.to)'
    
    for i in "${!insteads_from[@]}"; do
      local from="${CFG[${insteads_from[$i]}]}"
      local to="${CFG[${insteads_to[$i]}]}"
      echo "  ${from} -> ${to}"
    done
    ;;
  enable)
    _info "Enabling git insteadOf mappings for ${git_scope:--local} scope..."
    
    local insteads_from=()
    local insteads_to=()
    mapfile -t insteads_from < <(get_keys_grep_regex_from_cfg "git\.insteads\.[0-9]+\.from")
    mapfile -t insteads_to < <(get_keys_grep_regex_from_cfg "git\.insteads\.[0-9]+\.to")
    
    [[ "${#insteads_from[@]}" == "${#insteads_to[@]}" ]] || _err 'len(git.insteads.*.from) != len(git.insteads.*.to)'
    
    for i in "${!insteads_from[@]}"; do
      local from="${CFG[${insteads_from[$i]}]}"
      local to="${CFG[${insteads_to[$i]}]}"
      
      _info "Setting: url.${to}.insteadOf = ${from}"
      git config ${git_scope} "url.${to}.insteadOf" "${from}"
    done
    
    _info "Git insteadOf mappings enabled successfully"
    ;;
  
  disable)
    _info "Disabling git insteadOf mappings for ${git_scope:--local} scope..."
    
    local insteads_from=()
    local insteads_to=()
    mapfile -t insteads_from < <(get_keys_grep_regex_from_cfg "git\.insteads\.[0-9]+\.from")
    mapfile -t insteads_to < <(get_keys_grep_regex_from_cfg "git\.insteads\.[0-9]+\.to")
    
    [[ "${#insteads_from[@]}" == "${#insteads_to[@]}" ]] || _err 'len(git.insteads.*.from) != len(git.insteads.*.to)'
    
    for i in "${!insteads_from[@]}"; do
      local from="${CFG[${insteads_from[$i]}]}"
      local to="${CFG[${insteads_to[$i]}]}"
      
      _info "Removing: url.${to}.insteadOf"
      git config ${git_scope} --unset "url.${to}.insteadOf" 2>/dev/null || true
    done
    
    _info "Git insteadOf mappings disabled successfully"
    ;;
  
  *)
    git_usage
    _err "unknown subcmd of git instead: ${cmd}"
    ;;
  esac
}

execute_subcmd_git() {
  local cmd=${1:-""}

  case "${cmd}" in
  instead)
    execute_subcmd_git_instead "${@:2}"
    ;;
  *)
    git_usage
    _err "unknown subcmd of git: ${cmd}"
    ;;
  esac

}
